package com.yashi.appController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yashi.entity.TbGroup;
import com.yashi.entity.TbUser;
import com.yashi.entity.TbUserChat;
import com.yashi.service.GroupService;
import com.yashi.service.UserChatService;

@RestController
@RequestMapping("/group")
public class GroupController {
	@Autowired
	private GroupService groupService;
	@Autowired
	private UserChatService userChatDao;
	/**
	 * 查询群组聊天记录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/findGroupChatList")
	public Map<String, Object> finGroupChatList(HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		TbUserChat userChat = new TbUserChat();
		List<TbGroup> haveLastUserChatGroupList = new ArrayList<>();
		try {
			List<TbGroup> groupList = groupService.finGroupChatList(session);
			for (TbGroup tbGroup : groupList) {
				userChat.setReceiveId(tbGroup.getId());
				List<TbUserChat> lastGroupChat = userChatDao.selectLastGroupChat(userChat);
				tbGroup.setLastUserChat(lastGroupChat);
				haveLastUserChatGroupList.add(tbGroup);
			}
			map.put("data", haveLastUserChatGroupList);
			map.put("message", "查询成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 添加群
	 * 
	 * @param group
	 * @param session
	 * @return
	 */
	@RequestMapping("/save")
	public Map<String, Object> saveGroup(HttpServletRequest req,TbGroup group,String userList, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		List<String> userLists =new ArrayList<>();
		String arrayList []  = userList.split(",");
		for (String string : arrayList) {
			userLists.add(string);
		}
		try {
			Integer addGroup = groupService.addGroup(req,group,userLists,session);
			map.put("data", addGroup);
			System.out.println(userList);
			map.put("message", "添加成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("data", 0);
			map.put("message", "添加失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 修改群信息群
	 * 
	 * @param group
	 * @param session
	 */
	@RequestMapping("/update")
	public Map<String, Object> updateGroup(TbGroup group, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer updateById = groupService.updateById(group, session);
			map.put("data", updateById);
			map.put("message", "修改成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("data", 0);
			if(e.getMessage()!=null) {
				map.put("message", e.getMessage());
			}else {
				map.put("message", "服务器错误请联系管理员");
			}
			map.put("status", "0");
			return map;
		}

	}

	/**
	 * 删除群
	 * 
	 * @param group
	 * @param session
	 * id
	 */
	@RequestMapping("/delete")
	public Map<String, Object> deleteGroup(TbGroup group, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer deleteById = groupService.deleteById(group, session);
			map.put("data", deleteById);
			map.put("message", "删除成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("data", 0);
			if( e.getMessage()!=null) {
				map.put("message", e.getMessage());
			}else {
				map.put("message", "服务器错误请联系管理员");
			}
			map.put("status", "0");
			return map;
		}

	}

	/**
	 * 查看所有群
	 * 
	 * @param request
	 * @param group
	 * @return
	 */
	@RequestMapping("/getAllGroup")
	public Map<String, Object> getAllGroup() {
		Map<String, Object> map = new HashMap<>();
		try {
			List<TbGroup> groupList = groupService.getAllGroup();
			map.put("data", groupList);
			map.put("message", "查询成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "查询成功");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 根据群名称查询
	 * 
	 * @param request
	 * @param group
	 * @return
	 */
	@RequestMapping("/getGroupByName")
	public Map<String, Object> getGroupByName(String groupName) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<TbGroup> groupList = groupService.getGroupByName(groupName);
			map.put("data", groupList);
			map.put("message", "查询成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "查询失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 用户获取群组
	 * 
	 * @param request
	 * @param group
	 * @return
	 */
	@RequestMapping("/selectGroupByUserId")
	public Map<String, Object> selectGroupByUserId(HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<TbGroup> groupList = groupService.selectGroupByUserId(session);
			map.put("data", groupList);
			map.put("message", "查询成功");
			map.put("status", "1");
			return map;
			
		} catch (Exception e) {
			map.put("message", "查询失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 群组id获取用户
	 * 
	 * @param request
	 * @param group
	 * @return
	 */
	@RequestMapping("/selectUserbyGroupId")
	public Map<String, Object> selectUserbyGroupId(HttpSession session, String groupId) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<TbUser> userList = groupService.selectUserbyGroupId(groupId);
			map.put("data", userList);
			map.put("message", "查询成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "查询失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 添加群组成员
	 * 
	 * @param groupId
	 * @param userIdList
	 * @return
	 */
	@RequestMapping("/addUserToGroup")
	public Map<String, Object> addUserToGroup(String userListId,String groupId) {
		Map<String, Object> map = new HashMap<>();
		List<String> userLists =new ArrayList<>();
		String arrayList []  = userListId.split(",");
		for (String string : arrayList) {
			userLists.add(string);
		}
		try {
			Integer addUserToGroup = groupService.addUserToGroup(groupId,userLists);
			map.put("data", addUserToGroup);
			map.put("message", "添加成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "添加失败");
			map.put("status", "0");
			return map;
		}
	}

	/**
	 * 删除群成员
	 * 
	 * @param groupId
	 * @param userIdList
	 * @return
	 */
	@RequestMapping("/deleteUserToGroup")
	public Map<String, Object> deleteUserToGroup(String userListId,String groupId) {
		Map<String, Object> map = new HashMap<>();
		List<String> userLists =new ArrayList<>();
		String arrayList []  = userListId.split(",");
		for (String string : arrayList) {
			userLists.add(string);
		}
		try {
			Integer deleteUserToGroup = groupService.deleteUserToGroup(groupId, userLists);
			map.put("data", deleteUserToGroup);
			map.put("message", "删除成功");
			map.put("status", "1");
			return map;
		} catch (Exception e) {
			map.put("message", "删除失败");
			map.put("status", "0");
			return map;
		}
	}
	
	 /**
     * 实现多文件上传
     * 
     * 需要参数: 上传文件的变量名为：file  群组id:id
     * */
    @RequestMapping(value="/groupPhotoUpload") 
    public  Map<String,Object> groupPhotoUpload(HttpServletRequest request,TbGroup group){
    	Map<String,Object> map = new HashMap<>();
    	try {
    		List<TbGroup> userChatList = groupService.groupPhotoUpload(request,group);
    		map.put("data",userChatList.get(userChatList.size()-1).getPhoto());
    		map.put("message", "头像上传成功");
    		map.put("status", "1");
    		return map;
		} catch (Exception e) {
			map.put("message", "头像上传失败");
			map.put("status", "0");
			return map;
		}
    }

}
